#include "widget.h"
#include "ui_widget.h"
#include <iostream>
#include <fstream>
#include <string>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // ui->treeWidget->setHeaderLabel("动物");
    // QTreeWidgetItem* item1 = new QTreeWidgetItem();
    // item1->setText(0, "猫");
    // ui->treeWidget->insertTopLevelItem(0, item1);
    // QTreeWidgetItem* item2 = new QTreeWidgetItem();
    // item2->setText(0, "狗");
    // ui->treeWidget->insertTopLevelItem(1, item2);
    // QTreeWidgetItem* item3 = new QTreeWidgetItem();
    // item3->setText(0, "鸟");
    // ui->treeWidget->insertTopLevelItem(2, item3);
    // QTreeWidgetItem* item4 = new QTreeWidgetItem();
    // item4->setText(0, "中华田园猫");
    // item1->addChild(item4);
    std::ifstream file("E:/git/qt/TreeWidget_1/memory.txt");
    if(!file.is_open()) {
        qDebug() << "file open error";
    }
    auto dfs = [&](auto&& dfs, QTreeWidgetItem* parent, int n)->void
    {
        while(n--)
        {
            std::string name;
            int childCount;
            file >> name >> childCount;
            QTreeWidgetItem* item = new QTreeWidgetItem();
            item->setText(0, QString::fromStdString(name));
            parent->addChild(item);
            dfs(dfs, item, childCount);
        }
    };
    int TopLevelNums;
    file >> TreeWidgetName >> TopLevelNums;
    ui->treeWidget->setHeaderLabel(QString::fromStdString(TreeWidgetName));
    for(int i = 0; i < TopLevelNums; i++)
    {
        std::string name;
        int childCount;
        file >> name >> childCount;
        QTreeWidgetItem* item = new QTreeWidgetItem();
        item->setText(0, QString::fromStdString(name));
        ui->treeWidget->insertTopLevelItem(ui->treeWidget->topLevelItemCount(), item);
        dfs(dfs, item, childCount);
    }
    file.close();
}

Widget::~Widget()
{
    // qDebug() << "bye";
    std::ofstream file("E:/git/qt/TreeWidget_1/memory.txt");
    int TopLevelNums = ui->treeWidget->topLevelItemCount();
    file << TreeWidgetName << ' ' << TopLevelNums << '\n';
    auto dfs = [&](auto&& dfs, QTreeWidgetItem* parentItem)->void
    {
        int n = parentItem->childCount();
        for(int i = 0; i < n; i++)
        {
            QTreeWidgetItem* item = parentItem->child(i);
            std::string name = item->text(0).toStdString();
            int ChildCount = item->childCount();
            file << name << ' ' << ChildCount << '\n';
            if(ChildCount > 0)
                dfs(dfs, item);
        }
    };
    for(int i = 0; i < TopLevelNums; i++)
    {
        QTreeWidgetItem* item = ui->treeWidget->topLevelItem(i);
        std::string name = item->text(0).toStdString();
        int ChildCount = item->childCount();
        file << name << ' ' << ChildCount << '\n';
        if(ChildCount > 0)
            dfs(dfs, item);
    }
    delete ui;
}

void Widget::on_pushButton_insertTopLevelItem_clicked()
{
    const QString& text = ui->lineEdit->text();
    if(text.isEmpty()) return;
    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);
    ui->treeWidget->insertTopLevelItem(ui->treeWidget->topLevelItemCount(), item);
    ui->lineEdit->setText("");
}


void Widget::on_pushButton_insertItem_clicked()
{
    QTreeWidgetItem* currItem = ui->treeWidget->currentItem();
    const QString& text = ui->lineEdit->text();
    if(text.isEmpty() || currItem == nullptr) return;
    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);
    currItem->addChild(item);
    ui->lineEdit->setText("");
}


void Widget::on_pushButton_deleteItem_clicked()
{
    QTreeWidgetItem* currItem = ui->treeWidget->currentItem();
    if(currItem == nullptr) return;
    QTreeWidgetItem* parentItem = currItem->parent();
    if(parentItem == nullptr)
        ui->treeWidget->takeTopLevelItem(ui->treeWidget->indexOfTopLevelItem(currItem));
    else parentItem->removeChild(currItem);

}

